package com.dream.service.user.util;


import java.net.URI;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.Charset;

public class DesUtil {
	public static final int INT_PRIM_NUMBER = 95;

	public static final int INT_RETURN_LOOP = 94;

	public static final String CHAR_ENCODE = "UTF-8";

	/**
	 * 解密方法  strCode 需解密的字符串 解密后的字符串 1.0
	 * 加密方法对应着encode(String strOriginal)
	 */
	private static String decode(String strCode) {
		String strOriginal;
		int intRnd;
		String strRnd;
		int intStrLen;
		String strDecodeMe = "";

		if (strCode.equals(""))
			return strDecodeMe;
		intStrLen = strCode.length() - 1;

		strRnd = strCode.substring(intStrLen / 2, intStrLen / 2 + 1);
		intRnd = strRnd.hashCode() - "!".hashCode();
		strCode = strCode.substring(0, intStrLen / 2)
				+ strCode.substring(intStrLen / 2 + 1, intStrLen + 1);
		strOriginal = loopCode(strCode, INT_RETURN_LOOP
				- intRnd);
		strDecodeMe = strOriginal;
		return strDecodeMe;
	}

	/**
	 * 加密方法.随机取得加密的循环次数，使得每次加密所得的秘文会有所不同
	 * strOriginal 需加密的字符串 加密后的字符串 1.0
	 * 解密方法对应着decode(String strCode)
	 */
	private static String encode(String strOriginal) {
		String strCode;
		int intRnd;
		char rnd;
		int intStrLen;
		String strCodeMe = "";
		if (strOriginal.equals(""))
			return strCodeMe;
		// 2 到 93 之间的随即数，即同一原文可能获得93种不同的秘文
		intRnd = (int) (Math.random() * (INT_RETURN_LOOP - 2) + 2);
		strCode = loopCode(strOriginal, intRnd);
		// 对随机数作偏移加密
		rnd = (char) (intRnd +"!".hashCode());
		intStrLen = strCode.length();
		strCodeMe = strCode.substring(0, intStrLen / 2) + rnd
				+ strCode.substring(intStrLen / 2, intStrLen);
		if (strCodeMe.indexOf("??") >= 0) {
			return encode(strOriginal);
		}else {
			return strCodeMe;
		}
	}

	// 基础的凯撒算法,并对于每一位增加了偏移
	private static String kaiserCode(String strOriginal) {
		int intChar;
		String strCode;
		int i;
		int intStrLen;
		int intTmp;

		intStrLen = strOriginal.length();

		strCode = "";
		for (i = 0; i < intStrLen; i++) {
			intChar = strOriginal.substring(i, i + 1).hashCode();
			intTmp = intChar - "!".hashCode();
			intTmp = (intTmp * INT_PRIM_NUMBER + i + 1) % maxChar()
					+ "!".hashCode();
			strCode = strCode + (char) (intTmp);
		}
		return strCode;
	}

	// 循环调用凯撒算法一定次数后，可以取得原文
	private static String loopCode(String strOriginal, int intLoopCount) {
		String strCode = strOriginal;;
		int i;
		for (i = 0; i < intLoopCount; i++)
			strCode = kaiserCode(strCode);
		return strCode;
	}


	/**
	 *
	* 方法名:          decrypt
	* 方法功能描述:    解密，当前系统采用此方法
	* 加密方法对应着encrypt(String pw)
	* @param:
	* @return:
	 */
	public static String decrypt(String pw) {
		try {
			byte[] bt = new sun.misc.BASE64Decoder().decodeBuffer(pw);
			String pw2 = decode(new String(bt,Charset.forName(CHAR_ENCODE)));
			return pw2;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String encrypt(String pw) {
		try {
			String pw1 = encode(pw);
			String pw2  = new sun.misc.BASE64Encoder().encodeBuffer(pw1.getBytes(Charset.forName(CHAR_ENCODE)));
			return pw2;
		} catch (Exception e) {
		}
		return null;
	}

	private static int maxChar() {
		String str1 = "~";
		String str2 = "!";
		return str1.hashCode() - str2.hashCode() + 1;
	}
}
